/**
 * Copyright [2019] [LiBo/Alex of copyright liboware@gmail.com ]
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.generate.mybatisplus.handler;


import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.hyts.generate.mybatisplus.bean.AttributeFrameworkBean;
import com.hyts.generate.mybatisplus.bean.GenerateBean;
import com.hyts.generate.mybatisplus.config.DefaultAttributeConfig;
import com.hyts.generate.mybatisplus.handler.base.AbstractAttributeValueHandler;
import com.hyts.generate.mybatisplus.primtive.MpgConfiguration;

import java.util.Objects;
import java.util.Optional;

/**
 * @project-name:standard-boot
 * @package-name:com.hyts.standard.boot.generator.handler
 * @author:LiBo/Alex
 * @create-date:2019-07-26 15:14
 * @copyright:libo-alex4java
 * @email:liboware@gmail.com
 * @description:
 */
public class AttributeFrameworkHandler extends AbstractAttributeValueHandler<AttributeFrameworkBean> {

    /**
     * 创建对象操作服务
     * @param generateBean
     */
    public AttributeFrameworkHandler(GenerateBean generateBean) {
        super(generateBean);
        Objects.requireNonNull(Objects.isNull(generateBean),"操作服务创建对象不可以为空!");
    }

    /**
     * 构建器操作服务
      * @param param
     * @return
     */
    @Override
    public GenerateBean build(AttributeFrameworkBean param) {
        //转换操作服务
        Optional optional = Optional.of(param).map(frameworkBean-> {
            GlobalConfig globalConfig = new GlobalConfig();
            globalConfig.setAuthor((String) getValue(generateBean.getAuthor(), MpgConfiguration.GLOBAL_CONFIG.getAuthor()));
            globalConfig.setIdType((IdType) getValue(param.getIdType(),DefaultAttributeConfig.DEFAULT_ID_GENERATOR_TYPE));
            globalConfig.setActiveRecord((Boolean) getValue(param.isActiveRecord(),MpgConfiguration.GLOBAL_CONFIG.isActiveRecord()));
            globalConfig.setBaseColumnList((Boolean) getValue(param.isCreateColumnList(),MpgConfiguration.GLOBAL_CONFIG.isBaseColumnList()));
            globalConfig.setBaseResultMap((Boolean) getValue(param.isCreateResultMap(),MpgConfiguration.GLOBAL_CONFIG.isBaseResultMap()));
            globalConfig.setControllerName((String) getValue(param.getControllerNameSubfix(),DefaultAttributeConfig.DEFAULT_CONTROLLER_CLASS_SUBFIX));
            globalConfig.setDateType(DefaultAttributeConfig.DEFAULT_FIELD_DATE_TYPE);
            globalConfig.setEnableCache((Boolean) getValue(param.isEnable2LevelCache(),MpgConfiguration.GLOBAL_CONFIG.isEnableCache()));
            globalConfig.setKotlin((Boolean) getValue(param.isCreateKotlinCode(),MpgConfiguration.GLOBAL_CONFIG.isKotlin()));
            globalConfig.setFileOverride((Boolean) getValue(param.isFileOverride(),MpgConfiguration.GLOBAL_CONFIG.isFileOverride()));
            globalConfig.setOpen((Boolean) getValue(param.isOpen(),DefaultAttributeConfig.DEFAULT_OPEN_DIRECTORY_OUTPUT));
            globalConfig.setOutputDir((String) getValue(generateBean.getOutputDir(),DefaultAttributeConfig.PROJECT_PATH));
            globalConfig.setSwagger2((Boolean) getValue(param.isSwagger2(),DefaultAttributeConfig.DEFAULT_ENABLED_SWAGGER2));
            globalConfig.setMapperName((String) getValue(param.getMapperNameSubfix(),DefaultAttributeConfig.DEFAULT_MAPPER_CLASS_NAME_SUBFIX));
            globalConfig.setServiceImplName((String) getValue(param.getServiceImplNameSubfix(),DefaultAttributeConfig.DEFAULT_SERVICE_IMPL_CLASS_SUBFIX));
            globalConfig.setServiceName((String) getValue(param.getServiceNameSubfix(),DefaultAttributeConfig.DEFAULT_SERVICE_INTERFACE_CLASS_SUBFIX));
            globalConfig.setXmlName((String) getValue(param.getXmlNameSubfix(),DefaultAttributeConfig.DEFAULT_MAPPER_XML_NAME_SUBFIX));
            return globalConfig;
        });
        return generateBean.setGlobalConfig((com.
                baomidou.mybatisplus.generator.config.GlobalConfig) optional.get());
    }
}
